package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.KeyLocation;
	import flash.ui.Keyboard;
	
	[SWF(width="890", height="550")]
	public class JungleMania extends Sprite
	{
		private const WIDTH:uint = 890;
		
		public static var GAME_STARTED:Boolean;
		
		private var stakeComponent:Stake;
		private var rollTableCompnent:RollTable;
		
		private var boxes:Array;
		
		[Embed(source="assets/jungleCheesy.png")]
		private var jungle:Class;
		
		public function JungleMania()
		{
			var jungleSprite:Sprite = new Sprite();
			jungleSprite.addChild(new jungle());
			jungleSprite.x = 300;
			this.addChild(jungleSprite);
			
			stakeComponent = new Stake();
			stakeComponent.x = 890 - stakeComponent.width - 50;
			stakeComponent.y = 50;
//			this.addChild(stakeComponent);
			
			rollTableCompnent = new RollTable();
			rollTableCompnent.x = 50;
			rollTableCompnent.y = 50;
			this.addChild(rollTableCompnent);
			rollTableCompnent.addEventListener(RollTable.POTENTIAL_WIN, flashBoxes);
			
			// result boxes
			boxes = [];
			
			var boxOne:Box = new Box();
			boxOne.y = 300;
			boxOne.x = 60;
			boxOne.addEventListener(MouseEvent.CLICK, boxClicked);
			this.addChild(boxOne);
			boxes.push(boxOne);

			var boxTwo:Box = new Box();
			boxTwo.y = 300;
			boxTwo.x = 260;
			boxTwo.addEventListener(MouseEvent.CLICK, boxClicked);
			this.addChild(boxTwo);
			boxes.push(boxTwo);
			
			var boxThree:Box = new Box();
			boxThree.y = 300;
			boxThree.x = 460;
			boxThree.addEventListener(MouseEvent.CLICK, boxClicked);
			this.addChild(boxThree);
			boxes.push(boxThree);
			
			var boxFour:Box = new Box();
			boxFour.y = 300;
			boxFour.x = 660;
			boxFour.addEventListener(MouseEvent.CLICK, boxClicked);
			this.addChild(boxFour);
			boxes.push(boxFour);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
		}
		
		private function stopFlashingBoxes():void
		{
			for each (var b:Box in boxes)
			{
				if (!b.full() && b.hasFlashing)
				{
					b.stopFlash();
				}
			}
		}
		
		private function flashBoxes(e:Event):void
		{
			var potentialResult:Array = rollTableCompnent.getResult();
			
			for each (var b:Box in boxes)
			{
				if (!b.full())
				{
					b.flashPotetionalWin(potentialResult);
				}
			}
		}
		
		private function handleKeyDown(ke:KeyboardEvent):void
		{
			switch (ke.keyCode)
			{
				case Keyboard.SPACE:
					if (!GAME_STARTED)
					{
						stakeComponent.setNextStakeValue();
					}
					break;
				case Keyboard.ENTER:
//					setGameStarted();
//					
//					if (!rollTableCompnent.rolling)
//					{
//						rollTableCompnent.rollAll();
//					}else
//					{
//						rollTableCompnent.stopRollAll();	
//					}
					
					break;
				
				default:
					break;
			}
		}
		
		private function setGameStarted():void
		{
			if (!GAME_STARTED)
			{
				stakeComponent.toggleTab();	
				GAME_STARTED = true;
			}
		}
		
		public function boxClicked(event:MouseEvent):void
		{
			if (rollTableCompnent.rolling || !GAME_STARTED) return;
			
			var b:Box = event.currentTarget as Box;
			if (!b.full())
			{
				b.addResult(rollTableCompnent.getResult());	
				stopFlashingBoxes();
				rollTableCompnent.hideResult();
				rollTableCompnent.rollAll();
			}
		}
	}
}